package com.spring.shops.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spring.shops.dao.TzPickAddrMapper;
import com.spring.shops.model.dto.PickAddrDTO;
import com.spring.shops.model.entity.TzPickAddr;
import com.spring.shops.model.result.IPageIndexImg;
import com.spring.shops.model.result.ServerResponseEntityVoid;
import com.spring.shops.service.PickAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class PickAddrServiceImpl implements PickAddrService {
    @Autowired
    private TzPickAddrMapper mapper;
    /**
     * 新增自提点
     * @param tzPickAddr
     */
    @Override
    public boolean save(TzPickAddr tzPickAddr) {
        int rows = mapper.insert(tzPickAddr);
        if(rows>0){
            return true;
        }else return false;
    }
    /**
     * 分页查询
     * @param dto
     */
    @Override
    public ServerResponseEntityVoid<IPageIndexImg<TzPickAddr>> page(PickAddrDTO dto) {
        //获取当前页码
        Long currentPage = dto.getCurrent();
        //获取每页显示的条数
        Long size = dto.getSize();
        //设置分页初始值
        PageHelper.startPage(Math.toIntExact(currentPage), Math.toIntExact(size));
        //调用mapper层进行分页查询
        Page<TzPickAddr> pickAddrPage=mapper.page(dto);
        //创建IPageIndexImg对象
        IPageIndexImg<TzPickAddr> iPageIndexImg=new IPageIndexImg<>();
        iPageIndexImg.setSize(size);
        iPageIndexImg.setTotal(pickAddrPage.getTotal());
        iPageIndexImg.setCurrent(currentPage);
        iPageIndexImg.setPages(pickAddrPage.getTotal()/size+1);
        //获取所有记录
        List<TzPickAddr> result = pickAddrPage.getResult();
        iPageIndexImg.setRecords(result);
        return ServerResponseEntityVoid.success(iPageIndexImg);
    }
    /**
     * 批量删除数据
     * @param integers
     * @return
     */
    @Override
    public boolean deleteByIds(List<Integer> integers) {
        int rowAffected = mapper.deleteByIds(integers);
        if (rowAffected > 0) {
            return true;
        } else return false;
    }
    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @Override
    public ServerResponseEntityVoid<TzPickAddr> getById(Integer id) {
        TzPickAddr tzPickAddr = mapper.selectByPrimaryKey(Long.valueOf(id));
        return ServerResponseEntityVoid.success(tzPickAddr);
    }
    /**
     * 更新数据
     * @param tzPickAddr
     * @return
     */
    @Override
    public boolean update(TzPickAddr tzPickAddr) {
        int rows = mapper.updateByPrimaryKey(tzPickAddr);
        if(rows>0){
            return true;
        }else return false;
    }
}
